package com.zl.learning.arithmetic.my;

import java.util.Arrays;
import java.util.Random;

/**
 * 插入排序
 * <p>
 * 从后向前比，找到比自己小的位置然后插入进去
 */
public class InsertSort {
    private static int[] a = new Random().ints(0, 20).limit(7).toArray();

    private static void insertSort(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = i + 1; j > 0 && a[j] < a[j - 1]; j--) {
                int tmp = a[j];
                a[j] = a[j-1];
                a[j-1] = tmp;
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(a));
        insertSort(a);
        System.out.println(Arrays.toString(a));
    }

}
